﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.Components;
using System.Collections;
using System.Data;
using System.Text;

namespace FrameWork.web.Module.IntegralMS.IntegralMS.IntegralManager
{
    public partial class Index : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        public string CheckItem = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                LoadClass();
            }
        }
        void LoadClass()
        {
            QueryParam qp = new QueryParam();
            qp.Where = string.Format("where R_TeacherID={0}", UserData.GetUserDate.UserID);
            int recordCount = 0;
            ArrayList list = BusinessFacade.stu_ClassTeacherList(qp, out recordCount);
            ArrayList list1 = new ArrayList();
            foreach (stu_ClassTeacherRelationTable item in list)
            {
                list1.Add(BusinessFacade.stu_ClassInfoDisp(item.R_ClassID));
            }
            DropDownList1.DataSource = list1;
            DropDownList1.DataValueField = "ClassID";
            DropDownList1.DataTextField = "C_CName";
            DropDownList1.DataBind();
        }
        void LoadData()
        {
            QueryParam qp = new QueryParam();
            qp.Where = "where S_ClassID=" + Convert.ToInt32(string.IsNullOrEmpty(DropDownList1.SelectedValue) ? "0" : DropDownList1.SelectedValue);
            int RecordCount;
            ArrayList list = BusinessFacade.stu_StudentInfoList(qp, out RecordCount);
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
        //显示树图
        private void LoadTree()
        {
            QueryParam qp = new QueryParam();
            int RecordCount = 0;
            qp.Where = "Where C_Delete=0";
            qp.Orderfld = "C_Level,C_ShowOrder";
            qp.OrderType = 0;
            ArrayList list = BusinessFacade.stu_CheckItemList(qp, out RecordCount);
            foreach (stu_CheckItemTable item in list)
            {
                string checkItemID = item.CheckItemID.ToString();
                string name = item.C_CIName.ToString();
                int G_ChildCount = item.C_ChildCount;
                TreeNode node = new TreeNode();
                node.Value = checkItemID;
                node.Text = name;
                // node.Target = "user";
                // node.NavigateUrl = "User.aspx?ToId=1" + ViewState["ToId"] + "&ToName=1" + ViewState["ToName"] + "&GroupID=" + groupID + "&GroupName=" + groupName;
                node.SelectAction = TreeNodeSelectAction.Select;
                //TreeView1.Nodes.Add(node);
                if (G_ChildCount > 0) LoadNode(node, item.C_ParentID);
            }
        }
        //显示子单位
        private void LoadNode(TreeNode mainNode, int parentID)
        {
            QueryParam qp = new QueryParam(1, 5);
            int RecordCount = 0;
            qp.Where = string.Format("Where C_Delete=0 AND C_ParentID={0}", parentID);
            qp.Orderfld = "C_Level,C_ShowOrder";
            qp.OrderType = 0;
            ArrayList list = BusinessFacade.stu_CheckItemList(qp, out RecordCount);
            foreach (stu_CheckItemTable item in list)
            {
                string checkItemID = item.CheckItemID.ToString();
                string name = item.C_CIName.ToString();
                int C_ChildCount = item.C_ChildCount;
                TreeNode node = new TreeNode();
                node.Value = checkItemID;
                node.Text = name;
                //node.Target = "user";
                // node.NavigateUrl = "User.aspx?ToId=" + ViewState["ToId"] + "&ToName=" + ViewState["ToName"] + "&GroupID=" + groupID + "&GroupName=" + groupName;
                node.SelectAction = TreeNodeSelectAction.Select;
                mainNode.ChildNodes.Add(node);
                if (C_ChildCount > 0) LoadNode(node, item.C_ParentID);
            }

        }

        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            //CheckItem = TreeView1.SelectedNode.Text;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(DropDownList1.SelectedValue)) return;
            int classID = Convert.ToInt32(DropDownList1.SelectedValue);
            QueryParam qp = new QueryParam();
            qp.Where = string.Format("Where S_ClassID={0}", classID);
            int recordCount = 0;
            ArrayList list = BusinessFacade.stu_StudentInfoList(qp, out recordCount);
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
        public string GetScore()
        {
            string Where = string.Format("where S_StuID=CONVERT(VARCHAR(20),{0})", Eval("StuID"));
            stu_ScoreTable stu_Score = BusinessFacade.GetScoreDisp(Where);
            if (stu_Score != null)
            {
                return stu_Score.S_Score.ToString();
            }
            return "0";

        }
        protected void Button2_Click(object sender, EventArgs e)
        {

            StringBuilder sb1 = new StringBuilder("<font color='red'><b>" + DateTime.Now.ToString() + "</b></font><br/>");
            StringBuilder sb = new StringBuilder();
            int count = 0;
            if (bak.Value.Trim().Length > 25)
            {
                sb1.AppendLine("<font color='red'>error:说明字符多于25个字符</font><br/>");
            }
            else
            {
                sb.AppendLine("declare @error int");
                sb.AppendLine("declare @rowcount int");
                sb.AppendLine("set @error=0");
                sb.AppendLine("set @rowcount=0");
                sb.AppendLine("begin transaction");
                for (int i = 0; i < this.Repeater1.Items.Count; i++)
                {
                    if (this.Repeater1.Items[i].FindControl("cbSelect") is CheckBox)
                    {
                        CheckBox cb = this.Repeater1.Items[i].FindControl("cbSelect") as CheckBox;
                        if (cb.Checked)
                        {
                            string stuID = ((Label)this.Repeater1.Items[i].FindControl("StuID")).Text;
                            string stuName = ((Label)this.Repeater1.Items[i].FindControl("S_Name")).Text.Trim();
                            string ope = ((DropDownList)this.Repeater1.Items[i].FindControl("ddlType")).Text.Trim();
                            string value = ((TextBox)this.Repeater1.Items[i].FindControl("txtScore")).Text.Trim();
                            value = string.IsNullOrEmpty(value) ? "0" : value;
                            if (value == "0")
                                continue;
                            if (!Common.CheckRegEx(value, "^[0-9]*$"))
                            {
                                sb1.AppendLine("<font color='red'>error:积分必须为数字...</font><br/>");
                                break;
                            }
                            sb.AppendLine("if not exists(select * from stu_Score where S_StuID=CONVERT(varchar(20)," + stuID + "))");
                            sb.AppendLine("begin");
                            sb.AppendLine("insert into stu_Score values('" + stuID + "','" + value + "')");
                            sb.AppendLine("set @error=@error+@@error");
                            sb.AppendLine("set @rowcount=@rowcount+@@rowcount");
                            sb.AppendLine("end");
                            sb.AppendLine("else");
                            sb.AppendLine("begin");
                            sb.AppendLine("update stu_Score set S_Score=S_Score" + ope + value + " where S_StuID=CONVERT(varchar(20)," + stuID + ")");
                            sb.AppendLine("set @error=@error+@@error");
                            sb.AppendLine("set @rowcount=@rowcount+@@rowcount");
                            sb.AppendLine("insert into stu_ScoreChangeRecord values('" + bak.Value.Trim() + "','" + ope + value + "','" + UserData.GetUserDate.UserID + "','" + stuID + "',default)");
                            sb.AppendLine("set @error=@error+@@error");
                            sb.AppendLine("set @rowcount=@rowcount+@@rowcount");
                            sb.AppendLine("end");
                            sb1.AppendFormat("<font color='blue'>{0}</font>在{1}事件中，考核积分<font color='red'>{2}{3}</font><br/>", stuName, bak.Value.Trim(), ope, value);
                            count++;
                        }
                    }
                }
            }
            if (count == 0)
            {
               // sb1.AppendLine("没有选择任何行");
                sb1.AppendLine("<font color='green'><b>无更新......</b></font>");
            }
            else
            {
                sb.AppendLine("if(@error<>0)");
                sb.AppendLine("rollback transaction");
                sb.AppendLine("else");
                sb.AppendLine("commit transaction");
                sb.AppendLine("select @rowcount");
                int rowcount = BusinessFacade.GetRowCount(sb.ToString());
            }
            Literal1.Text = sb1.ToString();
            return;
        }
    }

}